<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="IEC104报文" prop="message">
        <el-input
          v-model="queryParams.message"
          placeholder="IEC104上传的总召(或变化上送)报文 多行用换行符分割"
          clearable
          type="textarea" rows="10"
          style="width: 800px;"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">解析</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <hr>

    <el-row v-if="this.uShow">
      <el-col :span="12" class="card-box">
        <el-card>
          <div slot="header"><span><i class="el-icon-cpu"></i> U帧</span></div>
          <div class="el-table el-table--enable-row-hover el-table--medium">
            <table cellspacing="0" style="width: 100%;">
              <thead>
              <tr>
                <th class="el-table__cell is-leaf">
                  <div class="cell">属性</div>
                </th>
                <th class="el-table__cell is-leaf">
                  <div class="cell">值</div>
                </th>
              </tr>
              </thead>
              <tbody>
              <tr>
                <td class="el-table__cell is-leaf">
                  <div class="cell">指令</div>
                </td>
                <td class="el-table__cell is-leaf">
                  <div class="cell" v-if="uFrame.command">{{ uFrame.command }}</div>
                </td>
              </tr>
              <tr>
                <td class="el-table__cell is-leaf">
                  <div class="cell">激活/确认</div>
                </td>
                <td class="el-table__cell is-leaf">
                  <div class="cell" v-if="uFrame.commandDirection">{{ uFrame.commandDirection }}</div>
                </td>
              </tr>
              </tbody>
            </table>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <el-row v-if="this.sShow">
      <el-col :span="12" class="card-box">
        <el-card>
          <div slot="header"><span><i class="el-icon-cpu"></i> S帧</span></div>
          <div class="el-table el-table--enable-row-hover el-table--medium">
            <table cellspacing="0" style="width: 100%;">
              <thead>
              <tr>
                <th class="el-table__cell is-leaf">
                  <div class="cell">属性</div>
                </th>
                <th class="el-table__cell is-leaf">
                  <div class="cell">值</div>
                </th>
              </tr>
              </thead>
              <tbody>
              <tr>
                <td class="el-table__cell is-leaf">
                  <div class="cell">发送序列号</div>
                </td>
                <td class="el-table__cell is-leaf">
                  <div class="cell" v-if="sFrame.sendNumber">{{ sFrame.sendNumber }}</div>
                </td>
              </tr>
              <tr>
                <td class="el-table__cell is-leaf">
                  <div class="cell">接收序列号</div>
                </td>
                <td class="el-table__cell is-leaf">
                  <div class="cell" v-if="sFrame.receiveNumber">{{ sFrame.receiveNumber }}</div>
                </td>
              </tr>
              </tbody>
            </table>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <el-row v-if="this.iShow">
      <el-col :span="12" class="card-box">
        <el-card>
          <div slot="header"><span><i class="el-icon-cpu"></i> I帧</span></div>
          <div class="el-table el-table--enable-row-hover el-table--medium">
            <table cellspacing="0" style="width: 100%;">
              <thead>
              <tr>
                <th class="el-table__cell is-leaf">
                  <div class="cell">属性</div>
                </th>
                <th class="el-table__cell is-leaf">
                  <div class="cell">值</div>
                </th>
              </tr>
              </thead>
              <tbody>
              <tr>
                <td class="el-table__cell is-leaf">
                  <div class="cell">发送序列号</div>
                </td>
                <td class="el-table__cell is-leaf">
                  <div class="cell" v-if="iFrame.sendNumber">{{ iFrame.sendNumber }}</div>
                </td>
              </tr>
              <tr>
                <td class="el-table__cell is-leaf">
                  <div class="cell">接收序列号</div>
                </td>
                <td class="el-table__cell is-leaf">
                  <div class="cell" v-if="iFrame.receiveNumber">{{ iFrame.receiveNumber }}</div>
                </td>
              </tr>
              <tr>
                <td class="el-table__cell is-leaf">
                  <div class="cell">数据类型</div>
                </td>
                <td class="el-table__cell is-leaf">
                  <div class="cell" v-if="iFrame.typeIdentification">{{ iFrame.typeIdentification }}</div>
                </td>
              </tr>
              <tr>
                <td class="el-table__cell is-leaf">
                  <div class="cell">数据类型名称</div>
                </td>
                <td class="el-table__cell is-leaf">
                  <div class="cell" v-if="iFrame.typeIdentificationName">{{ iFrame.typeIdentificationName }}</div>
                </td>
              </tr>
              <tr>
                <td class="el-table__cell is-leaf">
                  <div class="cell">传输原因</div>
                </td>
                <td class="el-table__cell is-leaf">
                  <div class="cell" v-if="iFrame.transmitReason">{{ iFrame.transmitReason }}</div>
                </td>
              </tr>
              <tr>
                <td class="el-table__cell is-leaf">
                  <div class="cell">客户端ID</div>
                </td>
                <td class="el-table__cell is-leaf">
                  <div class="cell" v-if="iFrame.clientId">{{ iFrame.clientId }}</div>
                </td>
              </tr>
              <tr>
                <td class="el-table__cell is-leaf">
                  <div class="cell">是否连续</div>
                </td>
                <td class="el-table__cell is-leaf">
                  <div class="cell" v-if="iFrame.series">{{ iFrame.series }}</div>
                </td>
              </tr>
              </tbody>
            </table>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <el-table v-if="this.iShow" v-loading="loading" :data="infoObjList">
      <el-table-column label="设备类型" prop="terminalType" align="center" width="150" />
      <el-table-column label="客户标识" prop="terminalId" align="center" width="200" />
      <el-table-column label="信息对象地址" prop="infoObjAddr" align="center" width="400"/>
      <el-table-column label="值" prop="value" align="center" width="160"/>
      <el-table-column label="时间戳" prop="timestamp" align="center" width="150"/>
    </el-table>

  </div>
</template>

<script>
import {getProtocolIec104} from "@/api/system/point_parser";

export default {
  name: "IEC104",
  dicts: ['sys_normal_disable'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 角色表格数据
      frameType: undefined,
      // U帧 S帧 I帧
      uFrame: {},
      sFrame: {},
      iFrame: {},
      uShow: false,
      iShow: false,
      sShow: false,
      infoObjList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 菜单列表
      menuOptions: [],
      // 查询参数
      queryParams: {
        message: ''
      },
      defaultProps: {
        children: "children",
        label: "label"
      }

    };
  },
  created() {
    this.loading = false;
    //this.queryData();
  },
  methods: {
    /** 查询iec104报文 */
    queryData() {
      this.loading = true;
      // 判断条件是否为空
      if (this.queryParams.message === '' || this.queryParams.message === undefined) {
        this.$alert("请输入IEC104上传的总召(或变化上送)报文", '系统提示', {
          dangerouslyUseHTMLString: true,
          type: 'success'
        });
        return;
      }
      getProtocolIec104(this.queryParams.message).then(response => {
          this.frameType = response.data.type
          if(response.data.type == 'I'){
            this.iShow = true;
            this.uShow = false;
            this.sShow = false;
          } else if(response.data.type == 'U'){
            this.uShow = true;
            this.iShow = false;
            this.sShow = false;
          } else if(response.data.type == 'S'){
            this.sShow = true;
            this.iShow = false;
            this.uShow = false;
          }
          this.uFrame = response.data.uframe
          this.sFrame = response.data.sframe
          this.iFrame = response.data.iframe
          console.log(this.iFrame)
          this.infoObjList = response.data.iframe.list
          this.loading = false;
        }
      );
    },
    // 取消按钮
    cancel() {
      this.open = false;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryData();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      //this.handleQuery();
      this.iShow = false;
      this.uShow = false;
      this.sShow = false;
    }
  }
};
</script>
